package com.chen.utils;

import com.alibaba.fastjson.JSONObject;
import com.chen.pojo.Song;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SongUtils {

    public static List<Song> getSongListByMsg(String text) {
        List<Song> list = new ArrayList<Song>();

        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpGet httpGet = new HttpGet("https://www.kuwo.cn/api/www/search/searchMusicBykeyWord?key=" + URLtoUTF8.toUtf8String(text) + "&pn=1&rn=20&httpsStatus=1&reqId=7234cbf0-add7-11ed-b4d7-21f64b81f17e");
        httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36");
        httpGet.addHeader("Referer", "https://www.kuwo.cn/search/list?key=%E6%9E%97%E4%BF%8A%E6%9D%B0");
        httpGet.addHeader("csrf", "XK3EKQYV1FS");
        httpGet.addHeader("Cookie", "_ga=GA1.2.1226683575.1676419745; Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1676419745,1676514174; _gid=GA1.2.1042470915.1676514174; gtoken=o4aqL0RurafQ; gid=8e0455db-69e6-41bf-b7f4-09f31f2b38a8; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1676531610; kw_token=XK3EKQYV1FS; _gat=1");
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpGet);
            HttpEntity entity = response.getEntity();

            String str = EntityUtils.toString(entity);
            //System.out.println(str);
            Map<String, Object> map = (Map<String, Object>) JSONObject.parse(str);
            //System.out.println(map.get("data"));
            Map<String, Object> map1 = (Map<String, Object>) map.get("data");

            List<Map<String, Object>> ls = (List<Map<String, Object>>) map1.get("list");
            for (Map<String, Object> sm : ls) {
                String duration = toTimeFormat((int)sm.get("duration"));
                list.add(new Song((String) sm.get("name"), (String) sm.get("artist"), (String) sm.get("album"), duration , (Integer)sm.get("rid") ));
            }
            return list;

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    public static String toTimeFormat(int t)
    {
        int h=t/3600;
        int m=t%3600/60;
        int s=t%60;
        String str="";
        if(h!=0)
        {
            if(h<10)
                str+="0"+h;
            else {
                str+=h;
            }
            str+=":";
        }
        if(m<10)
            str+="0"+m;
        else {
            str+=m;
        }
        str+=":";

        if(s<10)
            str+="0"+s;
        else {
            str+=s;
        }
        return str;
    }
}

